package com.keti35.login;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServelet
 */
public class LoginServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServelet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		String username = request.getParameter("username");
//		String password = request.getParameter("password");
//		
//		String jspName = "failue.jsp";
//		
//		if(username.equals(password)){
//			jspName ="sucess.jsp";
//		}
//		else {
//			request.setAttribute("loginName", username);
//			
//			jspName="failue.jsp";
//		}
//		request.getRequestDispatcher(jspName).forward(request,response);
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		String jspName = "failue.jsp";
		DBUtils dbUtils = new DBUtils();
		dbUtils.openConnection();
		String sql="select * from javamodeling_login"
		 + " where username ='" +username+ "'" +"and password= '" +password+ "'";
		ResultSet resultSet =dbUtils.selectQuery(sql);
		try {
			if(resultSet.next()){
				jspName="sucess.jsp";
			}
			else {
				jspName="failue.jsp";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dbUtils.closeConnection();
		
		//request.getRequestDispatcher(jspName).forward(request,response);
	
	}

}
